In [1]:
import plotly.graph_objects as go
from plotly.subplots import make_subplots
import pandas as pd  # Ensure pandas is imported
In [2]:
df1 = pd.read_csv(r'data/df1.csv')
df3p = pd.read_csv(r'data/df3p.csv')
df3n = pd.read_csv(r'data/df3n.csv')
evPickp = "Positive Event"
evPickn = "Negative Event"
In [3]:
def plot2subplots(d, df3p, df3n, evPickp, evPickn):
    import plotly.graph_objects as go
    from plotly.subplots import make_subplots
    import pandas as pd

    # Create a figure with 3 subplots and updated titles
    fig = make_subplots(rows=3, cols=1, shared_xaxes=True, vertical_spacing=0.1,
                        subplot_titles=("Bitcoin Price Returns", "Positive Event Conditional Distribution Probabilities",
                                        "Negative Event Conditional Distribution Probabilities"))

    d['Date'] = pd.to_datetime(d['Date'])

    # Subplot 1: Bitcoin Returns
    df_up_to_i = df1[df1['Date'] <= df1['Date'].iloc[len(d) - 1]]

    # Line trace for r_price with show legend set to False
    fig.add_trace(go.Scatter(x=df_up_to_i['Date'], y=df_up_to_i['r_price'], mode='lines', name='Bitcoin Returns',
                             line=dict(color='steelblue'), showlegend=False), row=1, col=1)

    # Markers for positive events
    event_indices_positive = df_up_to_i[df_up_to_i[evPickp] == 1].index
    fig.add_trace(go.Scatter(
        x=df_up_to_i.loc[event_indices_positive, 'Date'],
        y=df_up_to_i.loc[event_indices_positive, 'r_price'],
        mode='markers',
        marker=dict(color='blue', symbol='x', size=5),
        name=f'{evPickp}'), row=1, col=1)

    # Markers for negative events
    event_indices_negative = df_up_to_i[df_up_to_i[evPickn] == 1].index
    fig.add_trace(go.Scatter(
        x=df_up_to_i.loc[event_indices_negative, 'Date'],
        y=df_up_to_i.loc[event_indices_negative, 'r_price'],
        mode='markers',
        marker=dict(color='red', symbol='x', size=5),
        name=f'{evPickn}'), row=1, col=1)

    # Format date for title
    formatted_date = df1.at[len(df1) - 1, 'Date'].strftime('%d-%m-%Y')

    # Set title and axes labels for the first subplot
    fig.update_layout(title_text=f'Bitcoin Returns - Date: {formatted_date}',
                      title_x=0.5)

    # Subplot 2: Conditional Distribution Probability (Positive Events)
    df3p['Date'] = pd.to_datetime(df3p['Date'])
    fig.add_trace(go.Scatter(x=df3p['Date'], y=df3p['Probability'], mode='lines', name='Probability',
                             line=dict(color='steelblue'), showlegend=False), row=2, col=1)

    # Subplot 3: Conditional Distribution Probability (Negative Events)
    df3n['Date'] = pd.to_datetime(df3n['Date'])
    fig.add_trace(go.Scatter(x=df3n['Date'], y=df3n['Probability'], mode='lines', name='Probability',
                             line=dict(color='steelblue'), showlegend=False), row=3, col=1)

    # Update hover template to show full date for the subplots
    fig.update_traces(hovertemplate='Date: %{x|%Y-%m-%d}<br>Probability: %{y:.2f}', row=1, col=1)
    fig.update_traces(hovertemplate='Date: %{x|%Y-%m-%d}<br>Probability: %{y:.2f}', row=2, col=1)
    fig.update_traces(hovertemplate='Date: %{x|%Y-%m-%d}<br>Probability: %{y:.2f}', row=3, col=1)

    fig.update_xaxes(title_text="Date", row=1, col=1)
    fig.update_xaxes(title_text="Date", row=2, col=1)
    fig.update_xaxes(title_text="Date", row=3, col=1)

    fig.update_yaxes(title_text="Returns", row=1, col=1)
    fig.update_yaxes(title_text="Probability", row=2, col=1)
    fig.update_yaxes(title_text="Probability", row=3, col=1)

    # Update layout for the main title of the figure if needed
    fig.update_layout(title_text=f'Conditional Distribution {evPickp} Probability',
                      title_x=0.5,
                      width=1000,  # Set width of the figure (in pixels)
                      height=800)  # Set height of the figure (in pixels)

    # Update legend position for the first subplot (top right corner)
    fig.update_layout(legend=dict(
        x=0.99,
        y=0.99,
        xanchor='right',
        yanchor='top',
        orientation='h',
        traceorder='normal',
        bgcolor='rgba(255, 255, 255, 0.7)',
        bordercolor='black',
        borderwidth=1
    ))

    # Show the plot
    fig.show()
In [4]:
plot2subplots(df1, df3p, df3n, evPickp, evPickn)